package com.yoomiito.app.widget.dialog

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.utils.*

/**
 * 经纬度统一为火星经纬度
 */
class SelectMapDialog(context: Context,lat:Double,lng:Double) :BaseDialog(context){
    companion object{
        const val GaoDeMap = "高德地图"
        const val BaiDuMap = "百度地图"
        const val TengXunMap = "腾讯地图"
    }
    private val maps = mutableListOf<String>()
    private val mapPackage = mapOf("com.autonavi.minimap" to GaoDeMap,"com.baidu.BaiduMap" to BaiDuMap,"com.tencent.map" to TengXunMap)

    private val mAdapter = Adapter(null).also {
        it.setOnItemClickListener { adapter, view, position ->
            onSelectListener(it.data[position])
        }
    }
    private var onSelectListener:(String)->Unit={
        val url = when(it){
            GaoDeMap->{
//                val gps = GPSConverterUtils.gps84_To_Gcj02(lat,lng)
//                val converter = CoordinateConverter()
//                // CoordType.GPS 待转换坐标类型
//                converter.from(CoordinateConverter.CoordType.GPS)
//                // sourceLatLng待转换坐标点 DPoint类型
//                converter.coord(LatLng(lat,lng))
//                // 执行转换操作
//                val desLatLng: LatLng = converter.convert()
//                L.i("原始经纬度：lat:${lat}  lng: $lng")
//                L.i("火星经纬度：lat:${desLatLng.latitude}  lng: ${desLatLng.longitude}")

                "amapuri://route/plan/?sourceApplication=${ResUtil.getString(R.string.app_name)}&dlat=$lat&dlon=$lng&dev=0&t=0"
            }
            BaiDuMap->{
                "baidumap://map/direction?destination=$lat,$lng&coord_type=gcj02&src=andr.yoomiito.${ResUtil.getString(R.string.app_name)}"
            }
            TengXunMap->{
                "qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$lat,$lng&referer=VV2BZ-7EOCG-KS5QB-I5LLR-NIJRT-FZBT4"
            }
            else->{
                //调起浏览器
                //from=${39.994745},${39.994745},startpoint&
                "https://uri.amap.com/navigation?to=$lng,$lat,endpoint&mode=car&src=${ResUtil.getString(R.string.app_name)}"
            }
        }
        val intent = Intent()
        intent.action = Intent.ACTION_VIEW
        intent.addCategory(Intent.CATEGORY_BROWSABLE)
        intent.data = Uri.parse(url)
        context.startActivity(intent)
    }

    init {
        initMap()
    }

    override fun setView(): View {
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_select_map,null,false)
        view.findViewById<LinearLayout>(R.id.oo).background = ColorUtils.gradientAndRadius("#ffffff","#ffffff",20f,20f,0f,0f)
        view.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rcy).let {
            it.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
            val l = DensityUtil.dp2px(12f)
            it.setPadding(l,0,l,0)
            it.adapter = mAdapter
        }
        view.findViewById<ImageView>(R.id.close).setOnClickListener { dismiss() }
        mAdapter.setNewData(maps)
        setmGravity(Gravity.BOTTOM)
        setWidthPercent(1.0)
        setCanceledOnTouchOutside(true)
        return view
    }

    override fun show() {
        when{
            maps.isEmpty()->{
                onSelectListener("")
            }
            maps.size==1->{
                onSelectListener(maps[0])
            }
            else->{
                super.show()
            }
        }
    }




    private fun initMap() {
        maps.clear()
        mapPackage.forEach{
            if(DeviceConfigUtils.isAppInstalled(it.key)){
                maps.add(it.value)
            }
        }
    }

    inner class Adapter(data: List<String>?):BaseQuickAdapter<String,BaseViewHolder>(R.layout.item_div_text,data){

        override fun convert(helper: BaseViewHolder?, item: String?) {
            helper?.setText(R.id.tv,item)
        }
    }

}